package org.throwables;

import java.io.IOException;
import java.sql.SQLException;

import com.google.common.base.Throwables;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        try {
        	func();
        	
        	call();
        } catch (Exception e) {
        	e.printStackTrace();
        }
    }
    
    private static void func() throws Exception {
    	try {
        	System.out.println("----");	
        } catch (Throwable t) {
        	// 2.当Throwable类型为IOException时，才抛出异常
        	Throwables.throwIfInstanceOf(t, IOException.class);
        	
        	// 2.当Throwable类型为SQLException时，才抛出异常
        	Throwables.throwIfInstanceOf(t, SQLException.class);
        	
        	// 3.propagateIfPossible Throwable类型为X, Error或RuntimeException才抛出
        	Throwables.propagateIfPossible(t, SQLException.class);
        	
        	// 3.propagateIfPossible已废弃，使用throwIfUnchecked代替：Throwable类型为Error或RuntimeException才抛出
        	Throwables.throwIfUnchecked(t);
        	
        	// 1.propagate已废弃，使用throw e，或 throw new RuntimeException(e) 代替
        	// Throwables.propagate(t);
        }
    }
    
    public static void call() throws Exception {
    	try {
    		// TODO something
    	} catch (Throwable t) {
    		Throwables.propagateIfPossible(t, Exception.class);
    		throw new RuntimeException(t);
    	}
    }
}
